<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>已预约团票</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f9f9f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden; /* Prevent scrolling on the body itself */
        }

        .container {
            width: 393px;
            height: 853px;
            max-width: 100%;
            padding: 10px;
            box-sizing: border-box;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px;
            background-color: #ddd;
            border-bottom: 1px solid #ddd;
        }

        .header h1 {
            font-size: 18px;
            margin: 0;
            text-align: center;
        }

        .header button {
            padding: 8px 12px;
            border: none;
            background-color: #c1bebe;
            color: black;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .header button:hover {
            background-color: #0056b3;
        }

        .appointment-container {
            overflow-y: auto;
            flex-grow: 1; /* Allow the content area to expand and fill remaining space */
            padding: 10px 0;
        }

        .appointment {
            border: 1px solid #ddd;
            border-radius: 8px;
            margin-bottom: 15px;
            padding: 15px;
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .appointment img {
            width: 100%;
            height: 180px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 10px;
        }

        .appointment h3, .appointment p {
            margin: 0 0 5px;
            font-size: 14px;
            color: #333;
            text-align: center;
        }

        /* 取消按钮样式 */
        .cancel-btn {
            margin-top: 10px;
            padding: 8px 12px;
            border: none;
            background-color: #ddd;
            color: white;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .cancel-btn:hover {
            background-color: #ddd;
        }

        /* 弹出框的样式 */
        .confirmation-modal {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
            width: 300px;
            text-align: center;
            display: none; /* 默认不显示 */
            z-index: 9999; /* 确保它在其他元素之上 */
        }

        .confirmation-modal button {
            margin-top: 10px;
            padding: 8px 12px;
            border: none;
            background-color: #0056b3;
            color: white;
            border-radius: 4px;
            cursor: pointer;
        }

        .confirmation-modal button:hover {
            background-color: #004085;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>已预约团票</h1>
            <button onclick="window.location.href='/order'">返回</button>
        </div>
        <div class="appointment-container">
            {% for group, spot in groups %}
            <div class="appointment">
                <img src="{{ url_for('static', filename='images/' + spot.image_url) }}" alt="{{ spot.name }}">
                <h3>景区名称: {{ spot.name }}</h3>
                <p>团队人数: {{ group.team_size }}</p>
                <p>联系人姓名: {{ group.contact_name }}</p>
                <p>联系人电话: {{ group.contact_phone }}</p>
                <p>预约日期: {{ group.date }}</p>
                <p>预约时间: {{ group.time }}</p>
                <!-- 取消按钮 -->
                <button class="cancel-btn" onclick="confirmCancel({{ group.id }})">取消预约</button>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- 确认弹框 -->
    <div class="confirmation-modal" id="confirmation-modal">
        <p>您确定要取消预约吗？</p>
        <button onclick="cancelBooking()">确定</button>
        <button onclick="closeModal()">取消</button>
    </div>

    <script>
        let groupIdToDelete = null;

        // 显示确认取消的弹框
        function confirmCancel(groupId) {
            groupIdToDelete = groupId;
            document.getElementById('confirmation-modal').style.display = 'block';
        }

        // 关闭弹框
        function closeModal() {
            document.getElementById('confirmation-modal').style.display = 'none';
        }

        // 确认取消预约
        function cancelBooking() {
            if (groupIdToDelete) {
                fetch(`/cancel_group/${groupIdToDelete}`, { method: 'DELETE' })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            alert("预约已取消");
                            window.location.reload();  // 删除后刷新页面
                        } else {
                            alert("取消失败，请重试");
                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert("发生错误，请稍后再试");
                    });
            }
        }
    </script>
</body>
</html>
